package com.bc.bcapi.service;

import com.bc.bcmodel.domain.T_ProjectModel;
import com.bc.bcmodel.req.MngProductAuthReq;
import com.bc.commoncore.util.DateUtil;
import com.bc.commonentity.enums.SeqNameEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_Erc_contractMapper;
import com.bc.bcmodel.domain.T_Erc_contractModel;

/**
 * @Name T_Erc_contractService * @Description  * @Author generation * @Date 2018/10/18 23:42:20 * @Version 1.0
 **/
@Service
public class T_Erc_contractService {
    @Resource
    T_Erc_contractMapper t_Erc_contractMapper;
    @Resource
    T_ProjectService t_projectService;
    @Resource
    T_SequenceService t_sequenceService;

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_Erc_contractModel req) {
        return t_Erc_contractMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_Erc_contractModel> req) {
        return t_Erc_contractMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_Erc_contractModel findByNamedParam(T_Erc_contractModel req) {
        return t_Erc_contractMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_Erc_contractModel> getAllList(T_Erc_contractModel req) {
        return t_Erc_contractMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_Erc_contractModel> getList(T_Erc_contractModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Erc_contractModel> list = t_Erc_contractMapper.findByNamedParamList(req);
        return new PageInfo<T_Erc_contractModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_Erc_contractModel req) {
        return t_Erc_contractMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_Erc_contractMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_Erc_contractModel req) {
        return t_Erc_contractMapper.count(req);
    }

    /**
     * 判断当前项目是否写入合约表，不存在-创建
     */
    public boolean check(MngProductAuthReq req) {
        //String pid, int digit
        T_Erc_contractModel t_erc_contractModel = new T_Erc_contractModel();
        t_erc_contractModel.setPid(req.getPid());
        int count = t_Erc_contractMapper.count(t_erc_contractModel);
        t_erc_contractModel.setDigit(req.getDigit());
        if (count > 0) {
            //如果存在，更新合约表信息
            t_erc_contractModel.getModify_time(DateUtil.getToday());
            t_Erc_contractMapper.update(t_erc_contractModel);
            return true;
        }
        T_ProjectModel t_projectModel = new T_ProjectModel();
        t_projectModel.setPid(req.getPid());
        T_ProjectModel t_projectModelResult = t_projectService.findByNamedParam(t_projectModel);
        if (t_projectModelResult == null ||
                t_projectModelResult.getContract_eth_url().contains("*")) {
            return false;
        }
        t_erc_contractModel.setToken_symbol(t_projectModelResult.getTicker());
        t_erc_contractModel.setToken_name(t_projectModelResult.getIco_name());
        t_erc_contractModel.setContract_address(t_projectModelResult.getContract_eth_url());

        String contract_id = t_sequenceService.genSequence(SeqNameEnum.CONTRACT_ID);
        t_erc_contractModel.setContract_id(contract_id);
        insert(t_erc_contractModel);
        return true;
    }
}